package com.cruise.game.controller.user;

import com.cruise.game.common.api.ApiException;
import com.cruise.game.common.api.ApiResult;
import com.cruise.game.common.constants.CommonConstants;
import com.cruise.game.controller.BaseController;
import com.cruise.game.model.vo.common.PageResult;
import com.cruise.game.model.vo.user.FavoriteVO;
import com.cruise.game.service.user.FavoriteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 收藏控制器
 * 
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Api(tags = "收藏相关接口")
@RestController
@RequestMapping("/api/user/favorites")
public class FavoriteController extends BaseController {

    @Resource
    private FavoriteService favoriteService;

    /**
     * 添加收藏
     */
    @ApiOperation("添加收藏")
    @PostMapping
    public ApiResult<Boolean> addFavorite(
            @ApiParam(value = "服务ID", required = true) @RequestBody Map<String, Long> param) {
        Long serviceId = param.get("serviceId");
        if (serviceId == null) {
            return ApiResult.paramError("服务ID不能为空");
        }

        try {
            boolean result = favoriteService.addFavorite(getCurrentUserId(), serviceId);
            return ApiResult.success("收藏成功", result);
        } catch (ApiException e) {
            return ApiResult.fail(e.getMessage());
        } catch (Exception e) {
            log.error("添加收藏失败", e);
            return ApiResult.fail("收藏失败，请稍后重试");
        }
    }

    /**
     * 取消收藏
     */
    @ApiOperation("取消收藏")
    @DeleteMapping("/{serviceId}")
    public ApiResult<Boolean> cancelFavorite(
            @ApiParam(value = "服务ID", required = true) @PathVariable("serviceId") Long serviceId) {
        return ApiResult.success("已取消收藏", favoriteService.cancelFavorite(getCurrentUserId(), serviceId));
    }

    /**
     * 获取收藏列表
     */
    @ApiOperation("获取收藏列表")
    @GetMapping
    public ApiResult<PageResult<FavoriteVO>> getFavoriteList(
            @ApiParam(value = "页码", required = false, defaultValue = "1") @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,

            @ApiParam(value = "每页条数", required = false, defaultValue = "10") @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        // 限制最大页面大小
        if (pageSize > CommonConstants.MAX_PAGE_SIZE) {
            pageSize = CommonConstants.MAX_PAGE_SIZE;
        }

        return ApiResult.success(favoriteService.getFavoriteList(getCurrentUserId(), page, pageSize));
    }

    /**
     * 检查是否已收藏
     */
    @ApiOperation("检查是否已收藏")
    @GetMapping("/check/{serviceId}")
    public ApiResult<Map<String, Boolean>> isFavorite(
            @ApiParam(value = "服务ID", required = true) @PathVariable("serviceId") Long serviceId) {
        boolean isFavorite = favoriteService.isFavorite(getCurrentUserId(), serviceId);
        Map<String, Boolean> result = new HashMap<>();
        result.put("favorite", isFavorite);
        return ApiResult.success(result);
    }
}
